// 登录

<template>
  <div class="login">
    <div class="container">
      <img src="../assets/ku.png" alt class="avatar" />
      <!-- 组件 表单 -->
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input
            @focus="handleFocus('username')"
            prefix-icon="icon-user"
            v-model="loginForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            @focus="handleFocus('password')"
            prefix-icon="icon-key"
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-button class="login-btn" type="primary" @click="userLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/apis/user";
export default {
  data() {
    return {
      loginForm: {
        username: "10088",
        password: "000",
      },
      // 页面属性添加验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, message: "长度至少大于3个字符", trigger: "blur" },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 用户登录
    userLogin() {
      // 先进行用户输入的验证--二次验证，只有通过验证才能登录
      this.$refs.loginForm.validate(async (valid) => {
        // valid就是本次根据rules规则检验用户输入数据的结果
        // console.log(valid);
        // 验证成功才请求登录
        if (valid) {
          let res = await login(this.loginForm);
          console.log(res);
          if (res.data.statusCode == 401) {
            this.$message({
              message: res.data.message,
              type: "error",
            });
          } else {
            this.$message({
              message: res.data.message,
              type: "success",
            });
            // 登录成功，把token存储在本地
            localStorage.setItem("news_back_token", res.data.data.token);
            //跳转页面到首页
            setTimeout(() => {
              this.$router.push({ path: "/index" });
            }, 1000);
          }
        } else {
          // 验证失败
          this.$message({
            message: "数据输入不合法",
            type: "warning",
          });
          // 建议养好一个习惯：添加return false
          return false;
        }
      });
    },
    handleFocus(pn) {
      this.$refs.loginForm.clearValidate(pn);
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #392f41;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: -webkit-linear-gradient(
      to right,
      rgb(185, 43, 39),
      rgb(21, 101, 192)
    ); /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(
      to right,
      rgb(185, 43, 39),
      rgb(21, 101, 192)
    ); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>